﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;

using LuxDiner.Model.Results;
using LuxDiner.Model.Orders;
using LuxDiner.Logic.Orders;
namespace LuxDiner.Web.Management.Areas.Orders.Controllers
{
    [RouteArea("Orders")]
    [RoutePrefix("ProductOrder")]
    public class ProductOrderApiController : ApiController
    {
        [Route("SaveOrders")]
        [HttpPost]
        public JsonEntity<ProductOrderModel> SaveOrders(JObject request)
        {
            string errinfo = "对不起！单据保存失败，出错原因：";
            try
            {
                #region 收集参数
                var order = new ProductOrderModel() { Items = new ProductOrderDetailCollection() };
                var items = request.GetValue("orderItems");

                order.AssociatedDealID = request.Value<int?>("dealOrderID");
                order.NoteDate = request.Value<DateTime>("orderDate");
                order.HouseID = request.Value<int>("houseID");
                order.OperatorUserID = Web.Global.WebContext.CurrentUser.ID;
                order.Method = request.Value<int>("method");
                foreach (var it in items)
                {
                    order.Items.Add(new ProductOrderDetailModel()
                    {
                        ProductID = it.Value<int>("productID"),
                        CostMoney = it.Value<decimal>("costMoney"),
                        Quantity = it.Value<decimal>("quantity"),
                        Retention = it.Value<string>("retention")
                    });
                }
                #endregion

                var ret = ProductOrderLogic.NewOrder(order);
                return new JsonEntity<ProductOrderModel>(ret);
            }
            catch (Utils.LuxDinerException lde)
            {
                errinfo += lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntity<ProductOrderModel>(null, errinfo, false);
        }
        [Route("GetOrders")]
        [HttpPost]
        public JsonEntitySet<ProductOrderModel> GetOrders(JObject request)
        {
            string errinfo = "no data";
            try
            {
                #region 收集参数
                ProductOrderQuery sea = new ProductOrderQuery();
                sea.PageSize = request.Value<int>("pageSize");
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.GetCustomer = request.Value<bool>("getCustomer");
                sea.GetHouse = request.Value<bool>("getHouse");
                sea.CustomerID = request.Value<int>("customerID");
                sea.HouseID = request.Value<int>("houseID");
                sea.OrderNo = request.Value<string>("orderNo");
                sea.ProductID = request.Value<int>("productID");
                var odb = request.Value<string>("orderDateBefore");
                if (!string.IsNullOrEmpty(odb))
                    sea.OrderDateBefore = DateTime.Parse(odb);
                var oda = request.Value<string>("orderDateAfter");
                if (!string.IsNullOrEmpty(oda))
                    sea.OrderDateAfter = DateTime.Parse(oda);
                sea.Method = request.Value<int>("method");
                sea.GetDealOrder = request.Value<bool>("getDealOrder");
                #endregion

                var ret = ProductOrderLogic.GetOrders(sea);
                return new JsonEntitySet<ProductOrderModel>(ret);
            }
            catch (Utils.LuxDinerException lde)
            {
                errinfo = lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<ProductOrderModel>(null, errinfo, false);
        }
        [Route("SetBalance")]
        [HttpPost]
        public JsonEntity<ProductOrderModel> SetBalance(JObject request)
        {
            string errinfo = "对不起！单据保存失败，出错原因：";
            try
            {
                #region 收集参数
                var order = new ProductOrderModel() { Items = new ProductOrderDetailCollection() };
                var items = request.GetValue("orderItems");

                order.NoteDate = request.Value<DateTime>("orderDate");
                order.HouseID = request.Value<int>("houseID");
                order.OperatorUserID = Web.Global.WebContext.CurrentUser.ID;
                order.Method = request.Value<int>("orderMethod");
                var targetHouseID = request.Value<int>("targetHouseID");
                foreach (var it in items)
                {
                    order.Items.Add(new ProductOrderDetailModel()
                    {
                        ProductID = it.Value<int>("productID"),
                        CostMoney = it.Value<decimal>("costMoney"),
                        Quantity = it.Value<decimal>("quantity"),
                        Retention = it.Value<string>("retention")
                    });
                }
                #endregion

                var ret = ProductOrderLogic.SetBalance(order, targetHouseID);
                return new JsonEntity<ProductOrderModel>(ret);
            }
            catch (Utils.LuxDinerException lde)
            {
                errinfo += lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntity<ProductOrderModel>(null, errinfo, false);
        }
        [Route("GetOrderDetails")]
        [HttpPost]
        public JsonEntitySet<VProductOrderDetailsModel> GetOrderDetails(JObject request)
        {
            string errinfo = "no data";
            try
            {
                #region 收集参数
                ProductOrderQuery sea = new ProductOrderQuery();
                sea.PageSize = request.Value<int>("pageSize");
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.GetCustomer = request.Value<bool>("getCustomer");
                sea.GetHouse = request.Value<bool>("getHouse");
                sea.CustomerID = request.Value<int>("customerID");
                sea.HouseID = request.Value<int>("houseID");
                sea.OrderNo = request.Value<string>("orderNo");
                sea.ProductID = request.Value<int>("productID");
                var odb = request.Value<string>("orderDateBefore");
                if (!string.IsNullOrEmpty(odb))
                    sea.OrderDateBefore = DateTime.Parse(odb);
                var oda = request.Value<string>("orderDateAfter");
                if (!string.IsNullOrEmpty(oda))
                    sea.OrderDateAfter = DateTime.Parse(oda);
                sea.Method = request.Value<int>("method");
                #endregion

                var ret = ProductOrderLogic.GetOrderDetails(sea);
                return new JsonEntitySet<VProductOrderDetailsModel>(ret);
            }
            catch (Utils.LuxDinerException lde)
            {
                errinfo = lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<VProductOrderDetailsModel>(null, errinfo, false);
        }
    }
}
